package com.mdp.creational.singleton;

public class Singleton {
    // initialize later
    private static Singleton lazy_instance;
    
    public static Singleton getLazyInstance() { // double check
        if (lazy_instance == null) {
            try {
                synchronized (Class.forName(Singleton.class.getName())) {
                    if (lazy_instance == null) {
                        lazy_instance = new Singleton();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return lazy_instance;
    }
    
    // initialize immediately
    private static Singleton hunger_instance = new Singleton();
    
    public static Singleton getHungerInstance() {
        return hunger_instance;
    }
    
    private static class SingletonFactory {
        private static Singleton factory_instance = new Singleton();
    }
    
    public static Singleton getFactoryInstance() {
        return SingletonFactory.factory_instance;
    }
    
    private Singleton() {
        System.out.println("+++++++++++initalize singleton instance success++++++++++++");
    }
    
    public void outString() {
        System.out.println(getLazyInstance());
        System.out.println(getHungerInstance());
        System.out.println(getFactoryInstance());
    }
    
}
